app.controller("taskSaveCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "$filter", "lodash","id", function($scope, $rootScope, $modalInstance, $http, $filter, lodash,id) {
	$scope.info={};
    $scope.info.taskList=[];
    $scope.info.allUser=null;
    $scope.info.taskTypes=taskTypes;
    $scope.info.userList=[];


    $scope.getUserList=function(){
        var url=""

        if(!id){
            url=beetle_auth_url +"/user/?token_id=" + getTokenId()
        }else{
            url=matrix_product_url+"/task/getMemberTaskStats?projectPlanId="+id+"&tokenId="+getTokenId()
        }
         $http.get(url).success(function(data){
                if(data.success||data.code==0){
                    if(data.data){
                        $scope.info.userList = data.data;
                            angular.forEach($scope.info.userList , function(data){
                                data.crtname=data.name ? data.name : data.realname;
                            })
                            $scope.addTask(null);
                    }else{
                        $rootScope.toasterErrorInfo("获取用户数据错误！");
                    }
                }else{
                    $rootScope.toasterErrorInfo(data.msg ? data.msg  :data.desc);
                }
            })

    }
    $scope.getUserList();



    $scope.showSave=function(){
        var isTrue=false;
        angular.forEach($scope.info.taskList, function(data){
            if(data.content==null || data.content=="" || data.content==undefined){
                isTrue= true
            }
            if(data.workHours==null || data.workHours=="" || data.workHours==undefined){
                data.workHours=0;
            }

        })
        return isTrue;
    }


    $scope.addTask=function(i){
        var task={};
        task.projectPlanId=id;
        task.user=lodash.findWhere($scope.info.userList, {realname: getUserName()}) ? lodash.findWhere($scope.info.userList, {realname: getUserName()}) : $scope.info.userList[0]
        task.workHours=0;
        $http.get(matrix_product_url+"/task/getTaskNo").success(function(data){
            if(data.code==0){
                task.taskNo=data.data;
            }else{
                 $rootScope.showMessage("error", "失败", "生成项目编码失败");
                 return;
            }
        })
        $scope.info.taskList.splice(i+1, 0, task);
    }
    

    $scope.save=function(){
        var taskSaves=[]
        angular.forEach($scope.info.taskList, function(data){
            var task={}
            task.chargerAccount=data.user.account;
            task.chargerRealname=data.user.crtname;
            task.content=data.content;
            task.createrAccount=getUserAccount();
            task.createrRealname=getUserName();
            task.number=data.taskNo;
            task.projectPlanId=data.projectPlanId;
            task.type=data.type;
            task.workHours=data.workHours;
            taskSaves.push(task);
        })
        var obj={}
        obj.tasks=taskSaves;
        $.ajax({
                type: 'post',
                url: matrix_product_url + '/task/saveTasks',
                data: JSON.stringify(obj),
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.code == 0) {
                        $rootScope.toasterSuccessInfo("保存成功!");
                        $scope.cancel();
                    } else {
                        $rootScope.showMessage("error", "失败", data.desc)
                    }
                }
        })
    }
    $scope.delTask=function(i){
        if($scope.info.taskList.length==1){
            return false;
        }
        lodash.remove($scope.info.taskList, {taskNo: i.taskNo})
    }

    $scope.allUser=function(){
        angular.forEach($scope.info.taskList, function(task){
            task.user=$scope.info.allUser
        })
    }

    $scope.cancel=function(){
        $modalInstance.close();
    }

}])


app.controller("AloneTaskSaveCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "$filter", "lodash","id","taskInfo", function($scope, $rootScope, $modalInstance, $http, $filter, lodash,id,taskInfo) {
    $scope.info={};
    $scope.info.taskList=[];
    $scope.info.allUser=null;
    $scope.info.taskTypes=taskTypes;
    $scope.info.userList=[];
    $scope.info.task=taskInfo!=null ? taskInfo : {};
    $scope.getUserList=function(){
        var url=""

        if(!id){
            url=beetle_auth_url +"/user/?token_id=" + getTokenId()
        }else{
            url=matrix_product_url+"/task/getMemberTaskStats?projectPlanId="+id+"&tokenId="+getTokenId()
        }
         $http.get(url).success(function(data){
                if(data.success||data.code==0){
                    if(data.data){
                        $scope.info.userList = data.data;
                            angular.forEach($scope.info.userList , function(data){
                                data.crtname=data.name ? data.name : data.realname;
                            })
                    }else{
                        $rootScope.toasterErrorInfo("获取用户数据错误！");
                    }
                }else{
                    $rootScope.toasterErrorInfo(data.msg ? data.msg  :data.desc);
                }
            })

    }
    $scope.getUserList();
    var No="";
        var task={};
        if(taskInfo != null){
            $scope.info.task.user={
                "account"  : taskInfo.chargerAccount,
                "realname" : taskInfo.chargerRealname
            } 
            $scope.info.task.startupTime=taskInfo.startupTime;
        }else{
            $.ajax({
                type: "get",
                url: matrix_product_url+"/task/getTaskNo",
                async: false,
                success: function (data) {
                    No=data.data;
                }
            });
        }
        $scope.info.task.projectPlanId=id;

        $scope.info.task.workHours  = taskInfo != null ? taskInfo.workHours : 0;
        $scope.info.task.taskNo     = taskInfo != null ? taskInfo.number    : No;
        $scope.info.task.content    = taskInfo != null ? taskInfo.content   : "";
        $scope.info.task.id         = taskInfo != null ? taskInfo.id        : "";
        // $scope.info.taskList.splice(i+1, 0, task);
        // $scope.info.task=task;

    $scope.showSave=function(){
        var isTrue=false;
        angular.forEach($scope.info.taskList, function(data){
            if(data.content==null || data.content=="" || data.content==undefined || data.content.length > 20){
                isTrue= true
            }
            if(data.workHours==null || data.workHours=="" || data.workHours==undefined){
                data.workHours=0;
            }

        })
        return isTrue;
    }


    $scope.save=function(){
        var taskSaves=[]
            var task={}
            task.id = $scope.info.task.id ? $scope.info.task.id : "";
            task.chargerAccount=$scope.info.task.user.account;
            task.chargerRealname=$scope.info.task.user.realname;
            task.content=$scope.info.task.content;
            task.createrAccount=getUserAccount();
            task.createrRealname=getUserName();
            task.number=$scope.info.task.taskNo;
            task.projectPlanId=$scope.info.task.projectPlanId;
            task.type=$scope.info.task.type;
            task.workHours=$scope.info.task.workHours;
            task.startupTime=$scope.info.task.startupTime;
            taskSaves.push(task);
        var obj={}
        obj.tasks=taskSaves;
        $.ajax({
                type: 'post',
                url: matrix_product_url + '/task/saveTasks',
                data: JSON.stringify(obj),
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.code == 0) {
                        $rootScope.toasterSuccessInfo(taskInfo!=null ? "修改成功!" :"保存成功!");
                        $scope.cancel();
                    } else {
                        $rootScope.showMessage("error", "失败", data.desc)
                    }
                }
        })
    }
    $scope.delTask=function(i){
        if($scope.info.taskList.length==1){
            return false;
        }
        lodash.remove($scope.info.taskList, {taskNo: i.taskNo})
    }

    $scope.allUser=function(){
        angular.forEach($scope.info.taskList, function(task){
            task.user=$scope.info.allUser
        })
    }

    $scope.cancel=function(){
        $modalInstance.close();
    }

}])

